/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CreateCharacter.java
 *
 * Created on 08.11.2011, 19:10:14
 */
package earthdawnnet.creationism.character;

import earthdawnnet.server.shareable.charactersheet.CharacterSheet;
import earthdawnnet.util.IconProvider;
import java.awt.EventQueue;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFormattedTextField;
import org.jdesktop.application.Action;

/**
 *
 * @author Andreas Franek
 */
public class CreateCharacter extends javax.swing.JFrame
{

    /** Creates new form CreateCharacter */
    public CreateCharacter()
    {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        attributeSection = new earthdawnnet.creationism.character.AttributeSection();
        generalSection = new earthdawnnet.creationism.character.GeneralSection();
        appearanceSection = new earthdawnnet.creationism.character.AppearanceSection();
        createButton = new javax.swing.JButton();
        errorLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(earthdawnnet.client.EarthdawnNetApp.class).getContext().getResourceMap(CreateCharacter.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setIconImage(IconProvider.provide());
        setName("Form"); // NOI18N

        attributeSection.setName("attributeSection"); // NOI18N

        generalSection.setName("generalSection"); // NOI18N

        appearanceSection.setName("appearanceSection"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(earthdawnnet.client.EarthdawnNetApp.class).getContext().getActionMap(CreateCharacter.class, this);
        createButton.setAction(actionMap.get("create")); // NOI18N
        createButton.setText(resourceMap.getString("createButton.text")); // NOI18N
        createButton.setName("createButton"); // NOI18N

        errorLabel.setText(resourceMap.getString("errorLabel.text")); // NOI18N
        errorLabel.setName("errorLabel"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(generalSection, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(appearanceSection, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(attributeSection, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(26, 26, 26)
                        .addComponent(errorLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 268, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(createButton)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(generalSection, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(7, 7, 7)
                .addComponent(attributeSection, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addComponent(appearanceSection, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 35, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(createButton)
                    .addComponent(errorLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    public static void startup()
    {
//        /* Set the Nimbus look and feel */
          //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
//        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
//         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
//         */
//        try
//        {
//            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels())
//            {
//                if ("Nimbus".equals(info.getName()))
//                {
//                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
//                    break;
//                }
//            }
//        } catch (ClassNotFoundException ex)
//        {
//            java.util.logging.Logger.getLogger(CreateCharacter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (InstantiationException ex)
//        {
//            java.util.logging.Logger.getLogger(CreateCharacter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (IllegalAccessException ex)
//        {
//            java.util.logging.Logger.getLogger(CreateCharacter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (javax.swing.UnsupportedLookAndFeelException ex)
//        {
//            java.util.logging.Logger.getLogger(CreateCharacter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        }
          //</editor-fold>
//
        /* Create and display the form */
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                new CreateCharacter().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private earthdawnnet.creationism.character.AppearanceSection appearanceSection;
    private earthdawnnet.creationism.character.AttributeSection attributeSection;
    private javax.swing.JButton createButton;
    private javax.swing.JLabel errorLabel;
    private earthdawnnet.creationism.character.GeneralSection generalSection;
    // End of variables declaration//GEN-END:variables

    
    
    public static int checkStringNotEmpty(JFormattedTextField field) throws InvalidValueException
    {
        String result = field.getText();
        if(result.equalsIgnoreCase("")) throw new InvalidValueException();
        return Integer.valueOf(result);
    }

    @Action
    public void create()
    {
        CharacterSheet cs = new CharacterSheet();
        try
        {
            generalSection.putData(cs);
        } catch (InvalidValueException ex)
        {
            errorLabel.setText("General section was not filled out correctly");
            return;
        }
        try
        {
            attributeSection.putData(cs);
        } catch (InvalidValueException ex)
        {
            errorLabel.setText("Attributes section was not filled out correctly");
            return;
        }
                
        try
        {
            appearanceSection.putData(cs);
        } catch (InvalidValueException ex)
        {
            errorLabel.setText("Appearance section was not filled out correctly");
            return;
        }
        cs.save();
    }
}
